﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ThreadState = System.Threading.ThreadState;

namespace MINERP.ExportPhoto.Helper
{
    public class ThreadEx
    {
        public ThreadEx()
        { }

        private static Dictionary<object, ThreadEx> threadArr;
        public static ThreadEx StartTheadNow(Func<int> fun, int delay
                                              )
        {

            ThreadEx thread = new ThreadEx();
            thread._start = () => thread.RunNow(fun, delay);

           
            return thread;
        }
        public static ThreadEx StartThead(Action action, int delay
                                              )
        {

            ThreadEx thread = new ThreadEx();
            thread._start = () => thread.Run(action, delay);

           
            return thread;
        }
        private ThreadStart _start;
        public ThreadEx(ThreadStart start)
            : this()
        {
            _start = start;
        }
        private void RunNow(Func<int> fun, int msDelay)
        {
            while (true)
            {
                Thread.Sleep(500);
                try
                {

                    int rtn = fun();
                    if (rtn == 1)
                    {

                    }
                    else
                    {
                        Thread.Sleep(msDelay);
                    }

                }
                catch (Exception ex)
                {

                }
            }
            if (threadArr.ContainsValue(this))
            {
                object key = null;
                foreach (KeyValuePair<object, ThreadEx> keyValuePair in threadArr)
                {
                    if (keyValuePair.Value == this)
                    {
                        key = keyValuePair.Key;
                    }
                }
                if (key != null)
                {
                    threadArr.Remove(key);
                }
            }

        }
        private void Run(Action action, int msDelay)
        {
            while (true)
            {              
                try
                {
                    action();
                    Thread.Sleep(msDelay);

                }
                catch (Exception ex)
                {

                }
            }
        }
        public Thread BaseThread { get; set; }
        private ParameterizedThreadStart _pstart;
        public event ThreadExitEventHandler ThreadExit;
        private Action<object> _action;
        public void Start( params object[] parms)
        {
            BaseThread = new Thread(RunParameterMethod);
            BaseThread.IsBackground = true;
            if (parms == null)
            {
                BaseThread.Start();
            }
            else
            {
                BaseThread.Start(parms);
            }

        }
        private void RunParameterMethod(object state)
        {
            try
            {
                if (_pstart != null)
                {
                    _pstart.Invoke(state);
                }
                if (_start != null)
                {
                    _start.Invoke();
                }

            }
            catch (Exception ex)
            {

            }
            finally
            {
                InvokeThreadExit(state);
            }
        }
        private void InvokeThreadExit(object state)
        {
            ThreadExitEventHandler handler = ThreadExit;
            if (handler != null)
            {
                handler(this, state);
            }
        }
    }
    public delegate void ThreadExitEventHandler(ThreadEx sender, object state);
}
